package com.pyb.java.commonutil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.lang.reflect.Type;
import java.util.List;

public class JsonHelper {
    private static IParse iParse;

    public static void init(IParse parse) {
        iParse = parse;
    }

    public static String toJson(@NonNull Object bean) {
        return iParse.toJson(bean);
    }

    @Nullable
    public static <T> T fromJson(@NonNull String json, @NonNull Class<T> clazz) {
        return iParse.fromJson(json, clazz);
    }

    @Nullable
    public static <T> T fromJson(@NonNull String json, @NonNull Type type) {
        return iParse.fromJson(json, type);
    }

    public static <T> T fromJson(String json, TypeReference<T> typeReference) {
        return JSON.parseObject(json, typeReference);
    }

    public static <T> List<T> fromJsonByList(String json, Class<T> tClass) {
        return JSON.parseArray(json, tClass);
    }
}
